<?php

$all=$_GET["all"];

if (!$all) {$limit="limit 10";}
else {$limit="";}

include("mysql_conn.php");
include("settings.php");
$WEBAUTH_USER = getenv('WEBAUTH_USER');

function drawtableheader()
{
echo"<div id='tiny'>Click on the Project Title to edit the project<br>
Showing 10 most recent projects. <a href='myprojects.php?all=1'>Show everything</a></div>
<table width=85%>
<tr><th width=9%>Submitter</th><th>Title</th><th width=9%>Created</th><th width=16%>Original Due Date</th><th width=16%>Current Due Date</th><th width=9%>Last Updated</th></tr>";
}

function drawrestoftable($row)
{
 echo "<tr>";
 echo "<td>" .  $row['submitter'] . "</td>";
 echo "<td><a href='editproj.php?projectid=" . $row['projectid']. "'>" . $row['title'] . "</a></td>";
 echo "<td>" .  $row['created'] . "</td>";
 echo "<td>" .  $row['origduedate'] . "</td>";
 echo "<td>" .  $row['currduedate'] . "</td>";
 echo "<td>" .  $row['lastupdated_fmt'] . "</td>";
 echo "</tr>";
}

$adminquery=mysql_query("select admin from users where username='$WEBAUTH_USER'");

while($row=mysql_fetch_array($adminquery))
{
 $isadmin=$row['admin'];
}

// current projects
$result=mysql_query("select p.projectid,p.username,p.submitter,p.title,date_format(p.creationtime,'%m/%d/%Y') as 'created',date_format(p.origduedate,'%m/%d/%Y') as origduedate,date_format(p.currduedate,'%m/%d/%Y') as currduedate,p.state,date_format(u.updatetime,'%m/%d/%Y') as 'lastupdated_fmt' from projects p, updates u where username='$WEBAUTH_USER' and state='1' and u.projectid=p.projectid and u.updateid=(select max(updateid) from updates where projectid=p.projectid) order by updatetime desc $limit");
echo "<html><head><title>$site_title: My Projects</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><h2>My Projects</h2>
User: $WEBAUTH_USER<br>
<a href='newproj.php'>New Project</a>";
if ($isadmin)
{
 echo " | <a href='admin/index.html'>Admin Menu</a>";
}
echo " | <a href='myreports.php'>My Status Reports</a>";

echo "<p> 
<h3>Current Projects</h3>";
drawtableheader();

while($row=mysql_fetch_array($result))
{
 drawrestoftable($row);
}
echo "</table>";

// non-projects
$result4=mysql_query("select p.projectid,p.username,p.submitter,p.title,date_format(p.creationtime,'%m/%d/%Y') as 'created',date_format(p.origduedate,'%m/%d/%Y') as origduedate,date_format(p.currduedate,'%m/%d/%Y') as currduedate,p.state,date_format(u.updatetime,'%m/%d/%Y') as 'lastupdated_fmt' from projects p, updates u where username='$WEBAUTH_USER' and state='4' and u.projectid=p.projectid and u.updateid=(select max(updateid) from updates where projectid=p.projectid) order by updatetime desc");

echo "<h3>Non-Project Tasks</h3>
Everyone should have one  and only one project here.<br>";
drawtableheader();
while($row=mysql_fetch_array($result4))
{
 drawrestoftable($row);
}
echo "</table><p><hr><p>";

// non-current projects
$result2=mysql_query("select p.projectid,p.username,p.submitter,p.title,date_format(p.creationtime,'%m/%d/%Y') as 'created',date_format(p.origduedate,'%m/%d/%Y') as origduedate,date_format(p.currduedate,'%m/%d/%Y') as currduedate,p.state,date_format(u.updatetime,'%m/%d/%Y') as 'lastupdated_fmt' from projects p, updates u where username='$WEBAUTH_USER' and state='2' and u.projectid=p.projectid and u.updateid=(select max(updateid) from updates where projectid=p.projectid) order by updatetime desc $limit");


echo "<h3>Non-Current Projects</h3>";
drawtableheader();

while($row=mysql_fetch_array($result2))
{
 drawrestoftable($row);
}
echo "</table>";

// old projects
$result3=mysql_query("select p.projectid,p.username,p.submitter,p.title,date_format(p.creationtime,'%m/%d/%Y') as 'created',date_format(p.origduedate,'%m/%d/%Y') as origduedate,date_format(p.currduedate,'%m/%d/%Y') as currduedate,p.state,date_format(u.updatetime,'%m/%d/%Y') as 'lastupdated_fmt' from projects p, updates u where username='$WEBAUTH_USER' and state='3' and u.projectid=p.projectid and u.updateid=(select max(updateid) from updates where projectid=p.projectid) order by updatetime desc $limit");

echo "<h3>Old, Completed, or Dead Projects</h3>
Old Projects will not be reported on. Projects that are completed should go here.<br>";
drawtableheader();

while($row=mysql_fetch_array($result3))
{
 drawrestoftable($row);
}
echo "</table><p><hr><p>
<h3>Reporting</h3>
<ul>
<li><a href='userreport.php?username=$WEBAUTH_USER'>Preview Current Report</a></li>
<li><a href='savereport-mysql.php?username=$WEBAUTH_USER&mark=0'>Save current report snapshot to database</a> - Without altering projects or updates</li>
<li><a href='savereport-mysql.php?username=$WEBAUTH_USER&mark=1'>Save current report to database</a> - Also mark completed tasks as old and change current projects to non-current (Please preview first, this is an irreversible operation!)</li>
</ul>
<h4><a href='myreports.php'>My Status Reports</a></h4>
</body></html>";

mysql_close($con);
?>
 
